﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model;

using Jzy.DAL.Mail;
using System.Web.UI.WebControls;

using Jzy.Model.Mail;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Jzy.BLL.Mail
{
    public class WebMailAccountBO:CommonOperate<WebMailAccountInfo>
    {
        private int _userID;
        private WebMailAccountDAL _mailAccount;
        protected override BaseDAL DAL
        {
            get
            {
                return _mailAccount;
            }
        }
        public WebMailAccountBO(int userID):base("WebMailAccount")
        {
            _userID = userID;
            _mailAccount = new WebMailAccountDAL(userID);
        }
        public override WebMailAccountInfo GetInfo(object id)
        {
            WebMailAccountInfo info = base.GetInfo(id);
            DecryptAccount(info);
            return info;
        }
        public override void Save(WebMailAccountInfo info)
        {
            EncryptAccount(info);
            if (info.IsDefault)
            {
                _mailAccount.ClearDefault();
            }
            base.Save(info);
        }

        private static void EncryptAccount(WebMailAccountInfo info)
        {
            info.Pop3User = StrUtil.EncryptDES(info.Pop3User);
            info.Pop3Pwd = StrUtil.EncryptDES(info.Pop3Pwd);
            info.SmtpUser = StrUtil.EncryptDES(info.SmtpUser);
            info.SmtpPwd = StrUtil.EncryptDES(info.SmtpPwd);
        }

        private static void DecryptAccount(WebMailAccountInfo info)
        {
            info.Pop3User = StrUtil.DecryptDES(info.Pop3User);
            info.Pop3Pwd = StrUtil.DecryptDES(info.Pop3Pwd);
            info.SmtpUser = StrUtil.DecryptDES(info.SmtpUser);
            info.SmtpPwd = StrUtil.DecryptDES(info.SmtpPwd);
        }

        public override List<WebMailAccountInfo> GetList()
        {
            List< WebMailAccountInfo > list = _mailAccount.GetList();
            foreach (WebMailAccountInfo info in list)
            {
                DecryptAccount(info);
            }
            return list;
        }

        public WebMailAccountInfo GetDefaultInfo()
        {
            int id = _mailAccount.GetDefaultID();
            if (id == 0)
            {
                return null;
            }
            else
            {
                
                return GetInfo(id);
            }
        }

        public void BindDrp(DropDownList drp)
        {
            IEnumerable<WebMailAccountInfo> list = this.GetList();

            if (list.Count() > 0)
            {
                string selectValue = list.First().ID.ToString();
                foreach (WebMailAccountInfo info in list)
                {
                    ListItem item = new ListItem("\"" + info.DispName + "\"<" + info.MailAddr + ">", info.ID.ToString());
                    drp.Items.Add(item);
                    if (info.IsDefault)
                    {
                        selectValue = info.ID.ToString();
                    }
                }
                drp.SelectedValue = selectValue;
            }  
        }

        public int GetIDByAddr(string mailAddr)
        {
            int id = _mailAccount.GetIDByAddr(mailAddr);
            return id;
        }

        public static List<WebMailAccountInfo> GetAllList()
        {
            List<WebMailAccountInfo> list = WebMailAccountDAL.GetAllList();
            foreach (WebMailAccountInfo info in list)
            {
                DecryptAccount(info);
            }
            return list;
        }
    }
}
